package com.project.nimsen.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.project.nimsen.constant.ModelConstant;
import com.project.nimsen.dao.IBaseDao;
import com.project.nimsen.dto.ModelDto;
import com.project.nimsen.dto.SiteDto;
import com.project.nimsen.entity.Modelinfo;
import com.project.nimsen.service.IModelinfoService;
import com.project.nimsen.service.ISiteService;
import com.project.jdbc.dao.IBaseJdbcDao;
import com.project.jdbc.service.BaseServiceImpl;
import com.project.jdbc.utils.Finder;
@Repository("modelinfoService")
public class ModelinfoServiceImpl extends BaseServiceImpl implements
		IModelinfoService {
	@Resource
	ISiteService siteService;
	@Resource
	IBaseDao baseDao;
	protected IBaseJdbcDao getBaseDao() {
		return baseDao;
	}
	public List<Map<String, Object>> finder(ModelDto dto) throws Exception {
		Finder finder = new Finder("select mo.id,mo.model,mo.createUser,mo.createTime,"
			+ "mo.modeldesc,mo.active, us.username as createname,modelname from modelinfo as mo "
			+ "left join user as us on mo.createUser=us.id where mo.active!=1");
		if(StringUtils.isNotBlank(dto.getCreateUser())){
			finder.append(" and mo.createUser=:createUser");
			finder.setParam("createUser", dto.getCreateUser());
		}
		if(StringUtils.isNotBlank(dto.getModel())){
			finder.append(" and mo.model=:model");
			finder.setParam("model", dto.getModel());
		}
		finder.append(" order by mo.createTime desc");
		return baseDao.queryForList(finder, dto.getPage());
	}
	public Map<String, Object> findVilidate(ModelDto dto) throws Exception {
		if(dto==null)
			return null;
		Finder finder = new Finder("select id from modelinfo where active!=1");
		finder.append(" and ").append(dto.getValifield()).append("=:filed");
		finder.setParam("filed", dto.getValivalue());
		//如果id存在表示为修改
		if(StringUtils.isNotBlank(dto.getId())){
			finder.append(" and id!=:id");
			finder.setParam("id", dto.getId());
		}
		return baseDao.queryForObject(finder);
	}
	public boolean deleteById(String id) throws Exception {
		if(StringUtils.isBlank(id))
			return false;
		//得到要删除的信息
		Map<String, Object> model = findByid(id);
		if(model==null)
			return false;
		String modeltype = model.get("model").toString();
		//验证是否需要删除
		SiteDto dto = new SiteDto();
		if(ModelConstant.PAD.equals(modeltype))
			dto.setValifield("padmodelid");
		else if(ModelConstant.PHONE.equals(modeltype))
			dto.setValifield("phonemodelid");
		else
			dto.setValifield("modelid");
		dto.setValivalue(id);
		List<Map<String, Object>> models = siteService.find(dto);
		if(CollectionUtils.isNotEmpty(models))
			return false;
		//删除信息
		Finder finder = new Finder("update modelinfo set active=1 where active!=1 and id=:id");
		finder.setParam("id", id);
		return baseDao.update(finder)>0;
	}
	public Map<String, Object> findByid(String id) throws Exception {
		Finder finder = new Finder("select id,model,modeldesc,modelname,createUser,createTime"
			+ ",active from modelinfo where active!=1 and id=:id");
		finder.setParam("id", id);
		return baseDao.queryForObject(finder);
	}
	public Modelinfo findEntityById(String id) throws Exception {
		if(StringUtils.isBlank(id))
			return null;
		Finder finder = new Finder("select id,model,modeldesc,modelname,createUser,createTime,"
			+ "active from modelinfo where active!=1 and id=:id");
		finder.setParam("id", id);
		return baseDao.queryForObject(finder, Modelinfo.class);
	}
}
